package com.psa.tool;

import android.util.Log;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.io.File;
import java.util.HashMap;

/**
 *
 * @author david
 * @date 14-3-11
 */
public class Configurator {
    public Document getDocument() {
        return xmlHelper == null ? null : xmlHelper.getXmlDoc();
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        try{
            File file = new File(fileName);
            if (file.length()<=0) {
                file.delete();
            }

            if (!file.exists()){
                Document document = XmlHelper.createDocument("Configs");
                document.getDocumentElement().appendChild(XmlHelper.createNode(SystemStatus.PSA_CONFIG_SECTION, document));
                XmlHelper.write2Xml(document, file);
                file=null;
                xmlHelper = new XmlHelper(document);
            }else {
                file=null;
                xmlHelper = new XmlHelper(fileName);
            }

            initSettings();
        } catch (Exception e){
            Log.e(SystemStatus.TAG, "Class:Configurator,Msg:"+e.getMessage());
        }
    }

    private XmlHelper xmlHelper = null;
    private String fileName = "";
    private static Configurator configurator = null;
    private HashMap<String, String> _htSettings=null;

    private Configurator(){

    }

    public static Configurator getInstance(){
        if (configurator == null) {
            configurator=new Configurator();
        }
        return configurator;
    }

    public XmlHelper getXmlHelper(){
        return xmlHelper;
    }

    public String getValue(String path, boolean isXPath){
        if (isXPath) {
            return xmlHelper.getNodeValueByXPath(path);
        } else {
            return xmlHelper.getValueByPath(path, "");
        }
    }

    public String getValue(String path, String defValue){
        return xmlHelper.getValueByPath(path, defValue);
    }

    public String getValue(String path, boolean isXPath, String defValue){
        String s = getValue(path, isXPath);
        return s.equals("")?defValue:s;
    }

    public boolean getBooleanValue(String path, boolean isXPath, boolean defValue){
        String s = "";
        if (isXPath){
            s = xmlHelper.getNodeValueByXPath(path);
        } else {
            s = xmlHelper.getValueByPath(path, String.valueOf(defValue));
        }
        boolean b = defValue;
        if (!s.equals("")){
            try {
                b = Boolean.parseBoolean(s);
            } catch (Exception e) {
                Log.e(SystemStatus.TAG, e.getMessage());
                b = defValue;
            }
        }
        return b;
    }

    public int getIntegerValue(String path, boolean isXPath, int defValue){
        String s = "";
        if (isXPath){
            s = xmlHelper.getNodeValueByXPath(path);
        } else {
            s = xmlHelper.getValueByPath(path, String.valueOf(defValue));
        }
        int b = defValue;
        if (!s.equals("")){
            try {
                b = Integer.parseInt(s);
            } catch (Exception e) {
                Log.e(SystemStatus.TAG, e.getMessage());
                b = defValue;
            }
        }
        return b;
    }

    public long getLongValue(String path, boolean isXPath, long defValue) {
        String s = "";
        if (isXPath){
            s = xmlHelper.getNodeValueByXPath(path);
        } else {
            s = xmlHelper.getValueByPath(path, String.valueOf(defValue));
        }
        long b = defValue;
        if (!s.equals("")){
            try {
                b = Long.parseLong(s);
            } catch (Exception e) {
                Log.e(SystemStatus.TAG, e.getMessage());
                b = defValue;
            }
        }
        return b;
    }

    public void writeValue(String xpath, String value, boolean isXPath){
        try{
            if (isXPath) {
                xmlHelper.writeValueByXpath(xpath, value);
            } else {
                xmlHelper.writeValueByPath(xpath, value);
            }
        } catch (Exception e){

        }
    }

    public String GetSettingByName(String key){
        if (_htSettings==null) {
            return "";
        }
        try
        {
            if (_htSettings != null && _htSettings.containsKey(key)) {
                return _htSettings.get(key);
            } else {
                return "";
            }
        }
        catch (Exception e)
        {
            return "";
        }
    }

    private void initSettings(){
        NodeList nodeList = xmlHelper.getNodeListByXPath("//"+ SystemStatus.PSA_CONFIG_SECTION, null);
        if (nodeList==null || nodeList.getLength()<=0) {
            return;
        }
        _htSettings = new HashMap<String, String>();
        NodeList childs = nodeList.item(0).getChildNodes();
        int count = childs.getLength();
        for (int i=0;i<count;i++){
            if (childs.item(i).getNodeType()==Node.ELEMENT_NODE) {
                _htSettings.put(childs.item(i).getNodeName(), childs.item(i).getTextContent());
            }
        }
    }
}
